<template>
  <van-icon
    :name="value === 1 ? 'good-job' : 'good-job-o'"
    @click="onLike"
    :loading="loading"
    :color="value === 1 ? '#e5645f' : ''"
  />
</template>

<script>
import { likeArticleAPI, cancleLikeArticleAPI } from '@/api'
export default {
  name: 'liketArticle',
  data () {
    return {
      loading: false
    }
  },
  props: {
    value: {
      type: Number,
      required: true
    },
    userId: {
      type: [Number, String, Object],
      required: true
    }
  },
  methods: {
    async onLike () {
      console.log(typeof this.value)
      this.loading = true
      try {
        let status = -1
        if (this.value === 1) {
          // 已收藏，取消收藏
          await cancleLikeArticleAPI(this.userId)
        } else {
          // 未收藏，添加收藏
          await likeArticleAPI(this.userId)
          status = 1
        }
        // 更新视图
        this.$emit('input', status)
        this.$toast.success(status === 1 ? '点赞成功' : '取消点赞')
      } catch (error) {
        this.$toast.fail('操作失败，请重试！')
      }
      this.loadind = false
    }
  }
}
</script>

<style lang="less" scoped>
</style>
